<?php
defined('BASEPATH') or exit('No direct script access allowed');


class Businessnew extends MY_Controller
{


    public $DMH = "";

    public function __construct()
    {
        parent::__construct();
        if ($this->userinfo['group'] != 1 && $this->userinfo['group'] != 2) {
            $this->groupjunp($this->userinfo['group']);
        }
        $this->host = 'pdd';
        $this->address2 = '流快递更新时间：早上11点、晚上10点，当天的快递没出会在次日上午11点更新！！';
    }

    /**
     * 获取用户佣金配置信息
     */
    public function getcommissionbyuser()
    {
        $commission['1'] = $this->getcommissionconfig($this->project, $this->user_id, 1);
        $commission['2'] = $this->getcommissionconfig($this->project, $this->user_id, 2);
        $commission['3'] = $this->getcommissionconfig($this->project, $this->user_id, 3);
        $commission['4'] = $this->getcommissionconfig($this->project, $this->user_id, 4);
        for ($i = 0; $i < 8; $i++) {
            $row = [];
            foreach ($commission as $key => $vlaue) {
                $money = $vlaue['money'][$i] . "-" . $vlaue['money'][$i + 1];
                $gold = $vlaue['gold'][$i];

                $row[] = $money;
                $row[] = $gold;
            }
            $list[$i] = $row;
        }
        $msg['code'] = 0;
        $msg['list'] = $list;
        $this->sendjsonmsg($msg);
    }

    public function arrangement($row)
    {

        foreach ($row as $id => $v) {
            if ($id = 'keywords') {
                continue;
            }
            $row[$id] = trim($v);
        }
        $type_h = $row['type_h'];
        if (isset($row['gexin'])) {
            unset($row['gexin']);
        }
        if (isset($row['showpicnum'])) {
            unset($row['showpicnum']);
        }
        if (isset($row['comment'])) {
            unset($row['comment']);
        }
        $row['single_price'] = $row['pay_price'];

        if ($type_h != 1) {
            $row['chat'] = 0;
            $row['compare'] = 0;
            $row['collection'] = 0;
            $row['sign'] = 0;
            $row['overnight'] = 0;
            $row['sex'] = 0;
            $row['night'] = 0;
            $row['dianzan'] = 0;
        }
        $row['night'] = 0;
        if ($type_h != 2) {
            $row['showpic'] = 0;
            if (isset($row['showpicpath'])) {
                unset($row['showpicpath']);
            }
            if (isset($row['textpath'])) {
                unset($row['textpath']);
            }

        } else {
            if (isset($row['showpicpath'])) {
                if (!$row['showpicpath']) {
                    unset($row['showpicpath']);
                }

            }
            if (isset($row['textpath'])) {
                if (!$row['textpath']) {
                    unset($row['textpath']);
                }

            }
        }

        if ($row['endtime']) {
            $row['endtime'] = strtotime($row['endtime']);
        }

        return $row;
    }

    public function getconfig($name)
    {
        $res = parent::getconfig($name); // TODO: Change the autogenerated stub

        $msg['list'] = $res;
        $this->sendjsonmsg($msg);

    }

    /**
     * 发送检查
     * @param $row
     */
    public function sendchek($row)
    {


        if ($row['night'] == 1 && !$row['releasetime']) {
            $msg['code'] = 1;
            $msg['msg'] = '凌晨单必须提前定时发布';
            $this->sendjsonmsg($msg);
        }

        if ($row['releasetime']) {
            if ($row['night'] != 1) {
                if (strtotime($row['releasetime']) > strtotime(date('Y-m-d 21:00:00', strtotime($row['releasetime'])))) {
                    $msg['code'] = 1;
                    $msg['msg'] = '超过21点的定时任务无法发布';
                    $this->sendjsonmsg($msg);
                }
                if (strtotime($row['releasetime']) < strtotime(date('Y-m-d 07:00:00', strtotime($row['releasetime'])))) {
                    $msg['code'] = 1;
                    $msg['msg'] = '早于早上7点的定时任务无法发布';
                    $this->sendjsonmsg($msg);
                }
            } else {

                if (strtotime($row['releasetime']) > strtotime(date('Y-m-d 01:31:00', strtotime($row['releasetime'])))) {
                    $msg['code'] = 1;
                    $msg['msg'] = '凌晨单只能发布凌晨一点半前的任务';
                    $this->sendjsonmsg($msg);
                }

                if (strtotime($row['releasetime']) < (time() + 4 * 3600)) {
                    $msg['code'] = 1;
                    $msg['msg'] = '凌晨单必须提前4小时前发布';
                    $this->sendjsonmsg($msg);
                }


            }


            if ($row['type_h'] == 2) {
                if (strtotime($row['releasetime']) > strtotime(date('Y-m-d 00:00:00', time()))) {
                    if (strtotime($row['releasetime']) < strtotime(date('Y-m-d 23:59:59', time()))) {
                        $msg['code'] = 1;
                        $msg['msg'] = '高留评无法发布当天定时任务';
                        $this->sendjsonmsg($msg);
                    }
                }
            }
            if ($row['type_h'] == 3) {
                if (strtotime($row['releasetime']) > strtotime(date('Y-m-d 07:00:00', time()))) {
                    if (strtotime($row['releasetime']) < strtotime(date('Y-m-d 18:00:00', time()))) {
                        $msg['code'] = 1;
                        $msg['msg'] = 'DSR单无法在下午六点后发布';
                        $this->sendjsonmsg($msg);
                    }
                }
            }
            if (strtotime($row['releasetime']) > (time() + 3600 * 24 * 30)) {
                $msg['code'] = 1;
                $msg['msg'] = '不能发布超过一个月的定时任务';
                $this->sendjsonmsg($msg);
            }
        }

        if (time() > strtotime(date('Y-m-d 21:00:00')) && empty($row['releasetime'])) {
            $msg['code'] = 1;
            $msg['msg'] = '超过21点无法发布任务';
            $this->sendjsonmsg($msg);
        }
        if (time() > strtotime(date('Y-m-d 18:00:00')) && empty($row['releasetime']) && $row['type_h'] == 3) {
            $msg['code'] = 1;
            $msg['msg'] = '超过18点无法发布DSR任务';
            $this->sendjsonmsg($msg);
        }
        if (time() < strtotime(date('Y-m-d 07:00:00')) && empty($row['releasetime'])) {
            $msg['code'] = 1;
            $msg['msg'] = '7前点无法发布任务';
            $this->sendjsonmsg($msg);
        }
        $filedrow = ['coupon', 'remarks', 'pic', 'sku', 'keyword', 'releasetime', 'chat', 'compare', 'collection',
            'sign', 'overnight', 'sex', 'daifa', 'night', 'dianzan', 'showpicpath', 'textnum', 'showpic', 'textpath', 'endtime'];
        $this->load->model('Jd_task_jd_model');

        if (empty($row['picurl'])) {
            $msg['code'] = 1;
            $msg['msg'] = '请上传主图';
            $this->sendjsonmsg($msg);
        }
        foreach ($row as $id => $v) {
            if (empty($v) && !in_array($id, $filedrow)) {
                $msg['code'] = 1;
                $msg['msg'] = '缺少参数' . $id;
                $this->sendjsonmsg($msg);
            }
        }

    }


    /**
     * 获取佣金配置信息
     * @param int $perject 项目类型
     * @param int $type 任务类型
     */
    public function getcommissioninfo($perject = 1, $type = 1, $row = array())
    {
        $commission = $this->getcommissionconfig($perject, $this->user_id, $type);


        $additional = parent::getconfig('additional');
        if (!empty($row)) {
            $money = $row['pay_price'] * $row['each_single'];
            if ($type == 2 && $row['each_single'] == 2) {
                $money = $row['pay_price'];
            }
            $key = 0;
            foreach ($commission['money'] as $id => $v) {
                if ($money > $v) {
                    $key = $id;
                }
            }
            if (isset($commission['gold'][$key])) {
                $gold = $commission['gold'][$key];
            } else {
                $gold = $commission['gold'][$key - 1];
            }
            $gold = $gold + 1;
            $type_h = $row['type_h'];
            foreach ($additional as $id => $v) {
                if (isset($row[$id]) && $row[$id]) {
                    $gold = $gold + $v;
                }
            }
            $gold = round($gold, 2);

            return $gold;
        }

        $msg['code'] = 0;
        $msg['msg'] = '获取成功';
        $msg['list'] = $commission;
        $msg['additional'] = $additional;


        $this->sendjsonmsg($msg);
    }


    /**
     * 获取资金详细列表
     */
    public function getpaylog()
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if (empty($epage)) {
            $epage = 10;
        }
        $this->load->model('Jd_pay_log_model');
        $where = " where p1.user_id =" . intval($this->user_id);;
        $where .= $this->gettimesqlstr();
        $where .= $this->getsqlstrbyrow(['type']);
        $where .= $this->getsqlstrbyrow(['taskid', 'id', 'goods_name'], 'p3');
        $_select = " p1.*";
        $_order = " order by p1.id desc ";
        $sql = "select SELECT from `jd_pay_log` as p1
                left join  `jd_users` as p2  on   p1.user_id=p2.id      
                 left join  `jd_task_jd_list` as p3  on   p1.log_id=p3.id  AND p1.type=2     
				 SQL ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $where,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);
        $order['id'] = "desc";
        $result = $this->Jd_pay_log_model->get_server_list($_data);

        $typelist = parent::getconfig('paylog_type');
        foreach ($result['list'] as $id => $v) {
            $result['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $result['list'][$id]['type'] = $typelist[$v['type']];
            if ($v['type'] != 2) {
                $result['list'][$id]['number'] = "-";
            }
        }

        $this->sendjsonmsg($result);

    }


    /**
     * 获取充值列表
     */
    public function getpaylistaction()
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if (empty($epage)) {
            $epage = 10;
        }
        $where = "where 1=1 AND p1.user_id = " . intval($this->user_id);
        $where = $where . $this->getsqlstrbyrow(['status']);
        $where = $where . $this->getsqlstrbyrow(['id', 'username', 'phone'], "p2");
        $where = $where . $this->gettimesqlstr("p1.addtime");
        $this->load->model('Jd_pay_model');
        $_order = 'order by p1.id desc';
        $_select = "p1.*,p2.username,p2.recommender,p2.phone";
        $sql = "select SELECT from `jd_pay` as p1
                left join  `jd_users` as p2  on   p1.user_id=p2.id              
                             			   SQL    ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $where,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);

        $this->load->model('Jd_pay_model');
        $result = $this->Jd_pay_model->get_server_list($_data);

        $status = ['待审核', '已通过', '已拒绝'];
        foreach ($result['list'] as $id => $v) {
            $result['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $result['list'][$id]['picurl'] = base_url() . "public/uploads" . $v['picurl'];
            $result['list'][$id]['status'] = $status[$v['status']];
        }

        $this->sendjsonmsg($result);
    }

    public function addshopinfo()
    {

        $row = $this->input->post();
        $row['user_id'] = $this->user_id;
        $this->load->model('Jd_users_model');

        $row['webid'] = $this->web_id;
        $row['user_id'] = $this->user_id;
        $row['addtime'] = time();

        $res = $this->Jd_users_model->addshop($row);
        if ($res) {
            $msg['msg'] = "信息提交成功";
            $msg['code'] = 0;
            $this->sendjsonmsg($msg);
        } else {
            $msg['msg'] = "店铺名已存在";
            $msg['code'] = 1;
            $this->sendjsonmsg($msg);
        }

    }

    /**
     * 编辑店铺
     */
    public function editshop()
    {
        $this->load->model('Jd_users_model');
        $row = $this->input->post();
        $name = $row['name'];
        $r = $this->Jd_users_model->checkshop($name);
        if ($r) {
            $msg['code'] = 1;
            $msg['msg'] = "该店铺已经存在";
            $this->sendjsonmsg($msg);
        }

        $id = $row['id'];
        unset($row['id']);
        $r = $this->Jd_users_model->update('jd_shop', 'id', $id, $row);
        if (!$r) {
            $msg['code'] = 1;
            $msg['msg'] = "添加失败";
            $this->sendjsonmsg($msg);
        }
        $this->sendjsonmsg();
    }

    public function geshoplist()
    {
        $this->load->model('Jd_web_month_model');
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        $where = "  where 1=1   ";
        if ($this->group == 2) {
            $where = " where  p1.user_id= " . intval($this->user_id);
        }

        $where .= $this->getsqlstrbyrow(['webid', 'name', 'user_id', 'phone']);
        $where .= $this->getsqlstrbyrow(['username'], "p2");
        $where .= $this->gettimesqlstr();
        $where .= $this->gettimesqlstr('p1.update', 'update1', 'update2');
        if ($this->web_id == 1) {
            $_POST['webid'] = "全部";//搜全部站点
        }
        $row = $this->input->post();
        if (isset($row['status'])) {
            if ($row['status'] == 1) {
                $where .= " AND (p1.`status`=1 or p1.`status`=3) ";
            } else {
                if($row['status']){
                    $where .= " AND p1.`status`=" . intval($row['status']);
                }

            }
        }


        $_select = " p1.*,p2.username";
        $_order = " order by p1.id  desc ";
        $sql = "select SELECT from `jd_shop` as p1 
        left join jd_users as p2 on p1.user_id=p2.id    
				 SQL ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $where,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);
        $result = $this->Jd_web_month_model->get_server_list($_data);


        foreach ($result['list'] as $id => $v) {
            switch ($v['status']) {
                case 0:
                    $result['list'][$id]['status'] = '待审核';
                    break;
                case 1:
                    $result['list'][$id]['status'] = '已通过';
                    break;
                case 2:
                    $result['list'][$id]['status'] = '已拒绝';
                    break;
                case 3:
                    $result['list'][$id]['status'] = '已发奖励';
                    break;
            }
            $result['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $result['list'][$id]['update'] = $v['update'] ? date('Y-m-d H:i:s', $v['update']) : "";
        }

        $this->sendjsonmsg($result);

    }

    public function getDMH()
    {

        if ($this->DMH) {
            return $this->DMH;
        }
        $this->load->library('DMH');
        $DMH = new DMH();
        $this->DMH = $DMH;
        return $DMH;
    }

    /**
     * 获取api的Authorization
     */
    public function getapiheard()
    {
        $MYredis = $this->MyRedis();
        $key = "DMHAuthorization";
        $value = $MYredis->Get($key);
        $DMH = $this->getDMH();
        if (!$value) {
            $res = $DMH->login();
            $value = $res['Authorization'];
            $DMH->Authorization = $value;
            $MYredis->set($key, $value);
            $ttl = $MYredis->ttl($key);
            if ($ttl == -1) {
                $MYredis->expire($key, 60 * 60 * 3);
            }
        } else {
            $DMH->Authorization = $value;
        }
        return $value;
    }

    public function getPddInfo($id)
    {
        $DMH = $this->getDMH();

        $this->getapiheard();
        $DMH->apiurl = 'http://www.91zhangrenqi.com/python/getPddInfo/' . $id;
        $res = $DMH->SendaApiGET();
        echo $res;
        exit;
    }

    /**
     * 发布拼多多任务
     * @param array $row
     */
    public function SendPddTask($row = array())
    {
        if (empty($row)) {
            $row = $this->input->post();
        }

        $row = $this->arrangement($row);
        $row['type'] = 2;//拼多多任务
        $this->sendchek($row);
        $type_h = $row['type_h'];
        $singlemoney = $row['pay_price'] * $row['each_single'];
        $singlegold = $this->getcommissioninfo(1, $row['type_h'], $row);
        if ($type_h == 2 && isset($row['showpicpath'])) {
            if ($row['showpicpath']) {
                $row['showpic'] = $this->getpicnum($row['showpicpath']);
                if ($row['showpic'] < $row['number']) {
                    $msg['code'] = 1;
                    $msg['msg'] = '晒图图片数量小于单量';
                    $this->sendjsonmsg($msg);
                }
            }
        }
        if ($type_h == 2 && isset($row['textpath'])) {
            if ($row['textpath']) {
                if ($row['textnum'] < $row['number']) {
                    $msg['code'] = 1;
                    $msg['msg'] = '评论条数小于单量';
                    $this->sendjsonmsg($msg);
                }
            }
        }
        unset($row['textnum']);
        $allgold = $singlegold * $row['number'];
        /*if ($row['type_h'] == 1 && $this->is_vip) {//会员福利
            $goldnum = $this->getconfig("VIPfuli");
            $goldnum = $goldnum['goldnum'];
        } else {
            $goldnum = 0;
        }*/
        $goldnum = 0;
        $allgold = $allgold - $goldnum;
        $allgold = $allgold - $goldnum;
        $allmoney = $singlemoney * $row['number'];
        $hassend = $this->Jd_task_jd_model->getmytimetask($this->user_id);
        $hassendmoney = $hassend['money'] ? $hassend['money'] : 0;
        $hassendgold = $hassend['gold'] ? $hassend['gold'] : 0;
        $result = $this->checkmoney($allmoney + $hassendmoney, $allgold + $hassendgold);
        if (!$result) {
            $msg['code'] = 1;
            $msg['msg'] = '金币或者余额不足';
            $this->sendjsonmsg($msg);
        }

        if (isset($row['showpicpath'])) {
            unset($row['showpicpath']);
        }
        $keywords = $row['keywords'];
        unset($row['keywords']);
        if ($row['releasetime']) {
            $row['releasetime'] = strtotime($row['releasetime']);
            $this->db->trans_begin();
            foreach ($keywords as $id => $v) {
                $row['keyword'] = $v['keyword'];
                if (!$v['keyword'] || !$v['number']) {
                    continue;
                }
                $row['number'] = $v['number'] * 1;
                $row['gold'] = $singlegold * $v['number'];
                $result = $this->Jd_task_jd_model->addtask($row, 0);
                $this->saveshowpic($result);
                $this->savetext($result);
                if ($result) {
                    $newrow = array();
                    $newrow['frozenmoney'] = abs($row['number'] * $singlemoney);
                    $newrow['frozengold'] = abs($singlegold * $row['number']);;
                    $newrow["user_id"] = $this->user_id;
                    $this->frozenmoney($newrow);
                } else {
                    $this->db->trans_rollback();
                    $msg['code'] = 1;
                    $msg['msg'] = '定时任务发布失败';
                }
            }
            $msg['code'] = 0;
            $msg['msg'] = '定时任务发布成功';
            $this->db->trans_commit();
            $this->sendjsonmsg($msg);
        } else {
            unset($row['releasetime']);
        }
        $this->db->trans_begin();

        foreach ($keywords as $id => $v) {
            $row['keyword'] = $v['keyword'];
            if (!$v['keyword'] || !$v['number']) {
                continue;
            }
            $row['number'] = $v['number'] * 1;
            $row['gold'] = $singlegold * $v['number'];
            $taskid = $this->Jd_task_jd_model->addtask($row);
            if ($taskid) {
                $piclist = $this->saveshowpic($taskid);
                $textlist = $this->savetext($taskid);
                $array = $row;
                $array['taskid'] = $taskid;
                $array['list'] = $row['number'];
                $array['price'] = $row['pay_price'];
                $array['number'] = $row['each_single'];
                $array['gold'] = $singlegold;
                unset($array['pay_price']);
                unset($array['each_single']);
                unset($array['textpath']);
                $this->load->model('Jd_task_jd_list_model');
                $result2 = $this->Jd_task_jd_list_model->addtasklist($array, $this->user_id, $piclist, $textlist);
            } else {
                $this->db->trans_rollback();
                $msg['code'] = 1;
                $msg['msg'] = '添加任务失败';
                $this->sendjsonmsg($msg);
            }
            if ($result2) {//如果任务发布成功即开始
                $newrow['task_nub'] = $row['number'];
                $newrow['allgold'] = abs($allgold);;
                $newrow['allmoney'] = abs($allmoney);
                $this->UpdataUserinfo($newrow, $this->user_id);
                $orderlit = $this->Jd_task_jd_list_model->getlistidbytask(array($taskid));
                $result = false;
                foreach ($orderlit as $key => $value) {
                    $newrow = array();
                    $newrow['money'] = $singlemoney;
                    $newrow['gold'] = $singlegold;
                    $newrow["user_id"] = $this->user_id;
                    $result = $this->moneyupdate($newrow, 2, $value['id']);
                }
            } else {
                $this->db->trans_rollback();
                $msg['code'] = 1;
                $msg['msg'] = '子任务发布失败';
                $this->sendjsonmsg($msg);
            }
            if (!$result) {
                $this->db->trans_rollback();
                $msg['code'] = 1;
                $msg['msg'] = '任务信息日志更新失败';
                $this->sendjsonmsg($msg);
            }

            //$this->checkvip();
            //$this->checkInvitation();
        }

        $this->db->trans_commit();
        $msg['code'] = 0;
        $msg['msg'] = '任务发布成功';

        $this->ResetUserinfo();
        $this->sendjsonmsg($msg);
    }


    /**
     * 获取任务列表
     */
    public function gettasklist($type = 2, $show = 0, $issrot = false)
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        $getexcel = $this->input->post('getexcel') ? $this->input->post('getexcel') : false;//判断否要导出excel
        $time = time();
        $_sql = "where  p1.type=2 AND p1.business_id= " . intval($this->user_id);
        $gexing = $this->input->post('gexing') ? $this->input->post('gexing') : "";


        if ($gexing) {
            foreach ($gexing as $id => $v) {
                if ($id === 'textpath') {
                    $_sql .= " AND (p1.textpath <>'')";
                } else {
                    $_sql .= " AND p1." . $v . " >= 1";
                }
            }
        }


        $team = $this->input->post('team') ? $this->input->post('team') : "";

        if ($team) {
            $_sql .= " AND  locate($team,p1.`team`) ";
        }

        // echo $_sql;exit;

        $_sql = $_sql . $this->getsqlstrbyrow(['id', 'type_h'], "p1");
        $_sql = $_sql . $this->getsqlstrbyrow(['username'], "p2");
        $_sql = $_sql . $this->getsqlstrbyrow(['shop_name', 'keyword', 'status', 'webid']);
        $_sql = $_sql . $this->gettimesqlstr();
        $_select = " p1.*,p2.username,p2.salername";
        $_order = " order by p1.id desc ";
        $sql = "select SELECT from `jd_task_jd` as p1             
            left join jd_users as p2 on p2.id=p1.business_id 
				 SQL ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $_sql,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);

        $this->load->model('Jd_task_jd_model');
        $result = $this->Jd_task_jd_model->get_server_list($_data);
        $pay_type[1] = '全部开团';
        $pay_type[2] = '单独购买';
        $pay_type[3] = '优先参团';
        $type_hlist = parent::getconfig('type_h', 1);
        $statuslist = ["未发布", "已发布", "任务中", "已完成", "已废除"];
        foreach ($result['list'] as $id => $v) {
            $result['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $result['list'][$id]['money'] = round($v['pay_price'] * $v['number'] * $v['each_single'], 2);
            $result['list'][$id]['number'] = $v['finish_nub'] . "/" . $v['number'];
            $result['list'][$id]['Surplus'] = $v['number'] - $v['finish_nub'];
            $result['list'][$id]['type_hname'] = $type_hlist[$v['type_h']];
            $result['list'][$id]['releasetime'] = $v['releasetime'] ? date('Y-m-d H:i:s', $v['releasetime']) : "";
            @$result['list'][$id]['pay_type'] = $pay_type[$v['pay_type']];
            if ($v['releasetime'] && $v['status'] == 1 && $v['issend'] != 1) {
                $result['list'][$id]['status'] = 0;
            }
            $result['list'][$id]['releasetime'] = $v['releasetime'] ? date('Y-m-d H:i:s', $v['releasetime']) : "";
            $result['list'][$id]['statusname'] = $statuslist[$v['status']];
            $result['list'][$id]['team'] = $v['team'] ? explode(",", $v['team']) : [];
            $sexname = "";
            if ($v['sex'] == 1) {
                $sexname = "男号";
            }
            if ($v['sex'] == 2) {
                $sexname = "女号";
            }
            $result['list'][$id]['sexname'] = $sexname;
            $result['list'][$id]['special'] = 0;
            if ($v['chat'] > 0 || $v['compare'] > 0 || $v['collection'] > 0 || $v['sign'] > 0 || $v['overnight'] > 0 || $v['sex'] > 0 || $v['daifa'] > 0 || $v['textpath'] != "") {
                $result['list'][$id]['special'] = 1;
            }
            if ($v['dianzan'] > 0 || $v['night'] > 0) {
                $result['list'][$id]['special'] = 1;
            }
        }
        if ($getexcel) {
            $str['id'] = ['ID'];
            $str['addtime'] = ['加入时间', 20];
            $str['releasetime'] = ['定时', 20];
            $str['statusname'] = ['状态'];
            $str['type_hname'] = ['类型'];
            $str['goods_name'] = ['主商品', 70];
            $str['number'] = ['总单量'];
            $str['keyword'] = ['关键字', 30];
            $str['shop_name'] = ['商铺名', 20];
            $str['each_single'] = ['每单数量'];
            $str['pay_price'] = ['单个价格'];
            $str['pay_type'] = ['拼团'];
            $str['money'] = ['本金'];
            $str['gold'] = ['金币'];
            $this->excel_out2($str, $result['list'], "任务列表");
        }
        $this->sendjsonmsg($result);

    }


    /**
     * 获取订单列表
     */
    public function getorderlist($type = 1)
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        $getexcel = $this->input->post('getexcel');
        if (empty($epage)) {
            $epage = 10;
        }
        $this->load->model('Jd_task_jd_list_model');
        $shop_name = $this->input->post('shop_name') ? $this->input->post('shop_name') : "";

        $gexing = $this->input->post('gexing') ? $this->input->post('gexing') : "";

        $where = "where   p1.business_id= " . intval($this->user_id);


        if ($gexing) {
            foreach ($gexing as $id => $v) {
                $where .= " AND p1." . $v . " >= 1";
            }
        }


        $where = $where . $this->gettimesqlstr('p1.finishtime', "start", 'end');
        $where = $where . $this->gettimesqlstr('p1.addtime');

        $where = $where . $this->getsqlstrbyrow(['taskid', 'orderid', 'hunter_name', 'status', 'type_h', 'webid', 'id', 'expressorderid']);
        $where = $where . $this->getsqlstrbyrow(['username', 'recommender_id'], "p4");


        if (!empty($shop_name)) {
            $shop_name = trim($shop_name);
            $where .= " AND p1.shop_name like '%$shop_name%'";
        }


        $_select = " p1.*,p2.text";
        $_order = " order by p1.addtime desc ";
        $sql = "select SELECT from `jd_task_jd_list` as p1                   
             left join `jd_text` as p2 on p1.comment=p2.id             
				 SQL ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $where,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);

        $this->load->model('Jd_task_jd_model');
        $a = $this->Jd_task_jd_model->get_server_list($_data);
        $a['epage'] = $epage;
        $a['page'] = $page;
        $type_hlist = parent::getconfig('type_h', 1);
        $liststatus = ["未接取", "被接取", "已完成", '已废除', '已废除', '申请退款', '完成退款'];
        foreach ($a['list'] as $id => $v) {


            $a['list'][$id]['type_hname'] = $type_hlist[$v['type_h']];
            if ($v['finishtime']) {
                $a['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['finishtime']);
            } else {
                $a['list'][$id]['addtime'] = "";
            }

            if (!empty($v['update'])) {
                $a['list'][$id]['update'] = date('Y-m-d H:i:s', $v['update']);
            } else {
                $a['list'][$id]['update'] = "";
            }
            if ($v['chat'] > 0 || $v['compare'] > 0 || $v['collection'] > 0 || $v['sign'] > 0 || $v['overnight'] > 0 || $v['sex'] > 0 || $v['daifa'] > 0 || $v['comment'] > 0) {
                $a['list'][$id]['special'] = 1;
            }
            if ($v['dianzan'] > 0 || $v['night'] > 0) {
                $result['list'][$id]['special'] = 1;
            }
            $a['list'][$id]['money'] = $v['number'] * $v['price'];
            $a['list'][$id]['statusname'] = @$liststatus[$v['status']];

        }


        if ($getexcel) {
            $str['id'] = ['ID'];
            $str['addtime'] = ['完成时间', 20];
            $str['statusname'] = ['完成状态', 15];
            $str['orderid'] = ['订单号', 30];
            $str['money'] = ['总本金'];
            $str['real_price'] = ['实际付款'];
            $str['taskid'] = ['任务ID'];
            $str['shop_name'] = ['店铺名', 20];
            $str['goods_name'] = ['主商品', 70];
            $str['number'] = ['总单量'];
            $str['keyword'] = ['关键字', 30];
            $str['price'] = ['录入价格'];
            $str['number'] = ['每单数量'];
            $str['gold'] = ['金币'];
            $str['addrees'] = ['快递地址'];
            $str['express'] = ['快递公司'];
            $str['expressorderid'] = ['快递单号'];
            $this->excel_out2($str, $a['list'], "任务订单详情");
        }
        $this->sendjsonmsg($a);
    }

    public function getindexinfo()
    {

        $userid = intval($this->user_id);
        $sql = "select  count(1) as 'all',`status`
from jd_task_jd_list  
 where    business_id= $userid AND `type`=2 group  by `status`";
        $this->load->model('Jd_task_jd_list_model');
        $res = $this->Jd_task_jd_list_model->GetBySql($sql, 1);
        $order = array();
        foreach ($res as $id => $v) {
            $order[$v['status']] = $v['all'];
        }
        $sql = "select  count(1) as 'all',`status`
from jd_task_jd
 where     business_id= $userid AND `type`=2 group  by `status`";
        $res = $this->Jd_task_jd_list_model->GetBySql($sql, 1);

        $taskr = array();
        foreach ($res as $id => $v) {
            $taskr[$v['status']] = $v['all'];
        }
        $msg['code'] = 0;
        $msg['order'] = $order;
        $msg['task'] = $taskr;
        $this->sendjsonmsg($msg);

    }

    public function getbannerlist()
    {

        $this->load->model('Jd_banner_model');
        $list = $this->Jd_banner_model->getlist();


        $msg['code']=0;
        $msg['list']=$list;
        $this->sendjsonmsg($msg);
    }
}




